﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace Black.Minecraft.Editor.Behaviors
{
    /// <summary>
    /// Permet de se brancher sur les événments des storyboards
    /// </summary>
    public class StoryboardToCommandBehavior : Behavior<FrameworkElement>
    {
        #region Propriétés de dépendances

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(StoryboardToCommandBehavior), new UIPropertyMetadata(null));
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(StoryboardToCommandBehavior), new PropertyMetadata(null));
        public static readonly DependencyProperty StoryboardNameProperty = DependencyProperty.Register("StoryboardName", typeof(string), typeof(StoryboardToCommandBehavior), new UIPropertyMetadata(null));

        #endregion

        #region Propriétés

        /// <summary>
        /// Obtient ou défini la command a effectuer sur le completed du storyboard
        /// </summary>
        public ICommand Command
        {
            get
            {
                return ((ICommand)this.GetValue(StoryboardToCommandBehavior.CommandProperty));
            }
            set
            {
                this.SetValue(StoryboardToCommandBehavior.CommandProperty, value);
            }
        }


        /// <summary>
        /// Obtient ou définit le paramètre de la Command
        /// </summary>
        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }

            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }

        /// <summary>
        /// Obtient ou défini le nom du storyboard
        /// </summary>
        public string StoryboardName
        {
            get
            {
                return ((string)this.GetValue(StoryboardToCommandBehavior.StoryboardNameProperty));
            }
            set
            {
                this.SetValue(StoryboardToCommandBehavior.StoryboardNameProperty, value);
            }
        }

        #endregion

        #region Fonctions protégées

        /// <summary>
        /// Se produit sur l'attachement du behavior sur l'objet
        /// </summary>
        protected override void OnAttached()
        {
            Storyboard storyboard = this.AssociatedObject.FindResource(this.StoryboardName) as Storyboard;

            if (storyboard != null)
                storyboard.Completed += new EventHandler(this.StoryboardCompleted);
        }

        /// <summary>
        /// Se produit sur le détachement du behavior sur l'objet
        /// </summary>
        protected override void OnDetaching()
        {
            Storyboard storyboard = this.AssociatedObject.FindResource(this.StoryboardName) as Storyboard;

            if (storyboard != null)
                storyboard.Completed -= new EventHandler(this.StoryboardCompleted);
        }

        #endregion

        #region Fonctions privées

        /// <summary>
        /// Exécute la commande sur le completed du storyboard
        /// </summary>
        /// <param name="sender">Source de l'évènement.</param>
        /// <param name="e">Donnée de l'évènement.</param>
        private void StoryboardCompleted(object sender, EventArgs e)
        {
            if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
                this.Command.Execute(this.CommandParameter);
        }

        #endregion
    }
}
